package com.xinsoft.controller.device;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xinsoft.common.Response;
import com.xinsoft.entity.dto.CusUpkeepEntryParam;
import com.xinsoft.entity.po.CusUpkeepEntry;
import com.xinsoft.service.CusUpkeepEntryService;
import com.xinsoft.validator.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @ClassName CusUpkeepEntryController
 * @Description 保养条目控制层
 * @Author lijie
 * @Date 2023-6-13 12:53:16
 * @Version 1.0
 */
@RestController
@RequestMapping("/cus/upKeep/entry")
@Api(value = "CusUpKeepEntryController", tags = {"保养条目控制层"})
public class CusUpKeepEntryController {
    @Autowired
    private CusUpkeepEntryService upkeepEntryService;


    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 保养条目-新增
     * @Date 2023/5/17 9:57
     * @params dept
     **/
    @ApiOperation("新增")
    @PostMapping("/add")
    public Response<?> add(@RequestBody CusUpkeepEntry cusUpkeepEntry) {
        // 参数校验
        ValidatorUtils.validateEntity(cusUpkeepEntry);
        CusUpkeepEntry oldCusUpkeepEntry = upkeepEntryService.getOne(new QueryWrapper<CusUpkeepEntry>().eq("upkeep_name", cusUpkeepEntry.getUpkeepName()));
        if (oldCusUpkeepEntry != null) {
            return Response.fail("该项保养条目已存在,请不要重复添加");
        }
        return Response.succeed(upkeepEntryService.save(cusUpkeepEntry));
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 更新
     * @Date 2023/5/17 9:57
     * @params dept
     **/
    @ApiOperation("更新")
    @PostMapping("/edit")
    public Response<?> edit(@RequestBody CusUpkeepEntry cusUpkeepEntry) {
        // 参数校验
        ValidatorUtils.validateEntity(cusUpkeepEntry);
        CusUpkeepEntry oldCusUpkeepEntry = upkeepEntryService.getOne(new QueryWrapper<CusUpkeepEntry>().eq("upkeep_name", cusUpkeepEntry.getUpkeepName()));
        if (oldCusUpkeepEntry != null && !oldCusUpkeepEntry.getId().equals(cusUpkeepEntry.getId())) {
            return Response.fail("该项保养条目已存在,请不要重复添加");
        }
        return Response.succeed(upkeepEntryService.updateById(cusUpkeepEntry));
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 获取详情
     * @Date 2023/5/17 9:57
     * @params id
     **/
    @ApiOperation("获取详情")
    @GetMapping("/detail")
    public Response<?> detail(@RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
        return Response.succeed(upkeepEntryService.getById(id));
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 删除
     * @Date 2023/5/12 9:20
     * @params ids
     **/
    @ApiOperation("删除")
    @PostMapping("/del")
    public Response<?> del(@RequestBody List<Integer> ids) {
        upkeepEntryService.removeByIds(ids);
        return Response.succeed();
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 保养条目分页
     * @Date 2023/5/18 10:51
     * @params param
     **/
    @PostMapping("/pageList")
    public Response<?> pageList(@Valid @RequestBody CusUpkeepEntryParam param) {
        return Response.succeed(upkeepEntryService.findPageListByParam(param));
    }
}
